import 'dart:convert';
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:jxchmz/API/%E9%94%80%E5%94%AE%E7%AE%A1%E7%90%86/vipManage.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/GetxTools.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/KSProgressDialog.dart';

class VipAdd extends StatefulWidget {
  @override
  _VipAddState createState() => new _VipAddState();
}

class _VipAddState extends State<VipAdd> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  String phoneNum="";
var _groupValue = "1";
  //性别
  // List SexList = [{"Name":"男","id":"1"},{"Name":"女","id":"2"},{"Name":"保密","id":"3"}];
  // List<Widget> SexRadioList = [];
  //姓名
TextEditingController NameController = new TextEditingController();
  //手机号
  TextEditingController PhoneController = new TextEditingController();
  //身份证号
  TextEditingController IdCardController = new TextEditingController();
  //会员卡号
  TextEditingController VipNumController = new TextEditingController();
  //开卡日期
  TextEditingController OpenIdCardDateController = new TextEditingController();
//页面加载完成时
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    OpenIdCardDateController = new TextEditingController(text: DateTime.now().toString().substring(0,16));
    setState(() {
    });
  }
  ///页面卸载
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  ///当 State 被暂时从视图树中移除时，会调用这个方法，同时页面切换时，也会调用。
  @override
  void deactivate() {
    // TODO: implement dispose
    super.deactivate();
  }

  ///在 initState() 之后调用，当 State 对象的依赖关系发生变化时，该方法被调用，初始化时也会调用。
  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }

  // TODO: add state variables and methods
  @override
  Widget build(BuildContext context) {
    // TODO: add widget build method
    return  Scaffold(
      backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.white,
          title: Text("MembershipRegistration".tr, style: TextStyle(fontSize: 15, color: Colors.black),),
          centerTitle: true,
          elevation: 0,
          iconTheme: IconThemeData(
            color: Colors.black
          ),
        ),
        body: SingleChildScrollView(
          padding: EdgeInsets.only(left: 15,right: 15),
          child: Column(
            children: <Widget>[
              Form(
                key: _formKey,
                child: Column(
                  children: <Widget>[
                    Row(
                      children: [
                        Container(
                          color:Color(0xff4990e2),
                          width: 5,
                          height: 15,
                          child: Text("1",style: TextStyle(fontSize: 5,color: Color(0xff4990e2)),),
                        ),
                        Container(
                          width: 5,
                        ),
                        Container(
                          child: Text("Userinformation".tr,style: TextStyle(fontSize: 15),),
                        ),
                      ],
                    ),
                    
                    TextFormField(
                      decoration: InputDecoration(
                        hintText: 'name'.tr,
                        hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                        prefixIcon: Icon(Icons.perm_identity),
                        enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.black12),
                        ),
                      ),
                      controller: NameController,
                      validator: (value) {
                        if (value!.isEmpty) {
                          return 'Pleaseenteryourname'.tr;
                        }
                        return null;
                      },
                    ),
                    TextFormField(
                      controller: PhoneController,
                      decoration: InputDecoration(
                        hintText: 'Mobilephonenumber'.tr,
                          hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                          prefixIcon: Icon(Icons.phone_android),
                        enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.black12),
                        ),
                      ),
                      validator: (value) {
                        RegExp reg = new RegExp(r'^\d{11}$');
                        if (!reg.hasMatch(value!)) {
                          return 'Pleaseenteran11-digitmobilephonenumber'.tr;
                        }
                        return null;
                      },
                    ),
                    TextFormField(
                      controller: IdCardController,
                      decoration: InputDecoration(
                        hintText: 'IDnumber'.tr,
                        hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                        prefixIcon: Icon(Icons.account_box_rounded),
                        enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.black12),
                        ),
                      ),
                    ),
                    Container(
                      height: 15,
                    ),
                    Row(
                      children: [
                        Container(
                          color:Color(0xff4990e2),
                          width: 5,
                          height: 15,
                          child: Text("1",style: TextStyle(fontSize: 5,color: Color(0xff4990e2)),),
                        ),
                        Container(
                          width: 5,
                        ),
                        Container(
                          child: Text("gender".tr),
                        ),
                      ],
                    ),
                    Row(
                      children: [
                        Row(
                          children: [
                            Radio(
                                value: "1",
                                activeColor: Colors.blue,
                                groupValue: _groupValue,
                                onChanged: (value) {
                                  setState(() {
                                    _groupValue = value.toString();
                                  });
                                }),
                            Text("man".tr)
                          ],
                        ),
                        Row(
                          children: [
                            Radio(
                                value: "2",
                                activeColor: Colors.blue,
                                groupValue: _groupValue,
                                onChanged: (value) {
                                  setState(() {
                                    _groupValue = value.toString();
                                  });
                                }),
                            Text("woman".tr)
                          ],
                        ),
                        Row(
                          children: [
                            Radio(
                                value: "3",
                                activeColor: Colors.blue,
                                groupValue: _groupValue,
                                onChanged: (value) {
                                  setState(() {
                                    _groupValue = value.toString();
                                  });
                                }),
                            Text("secrecy".tr)
                          ],
                        ),
                      ],
                    ),
                    Container(
                      height: 15,
                    ),
                    Row(
                      children: [
                        Container(
                          color:Color(0xff4990e2),
                          width: 5,
                          height: 15,
                          child: Text("1",style: TextStyle(fontSize: 5,color: Color(0xff4990e2)),),
                        ),
                        Container(
                          width: 5,
                        ),
                        Container(
                          child: Text("Cardnumberinformation".tr),
                        ),
                      ],
                    ),
                    TextFormField(
                      controller: VipNumController,
                      decoration: InputDecoration(
                        hintText: 'Membershipcardnumber'.tr,
                          hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                          prefixIcon: Icon(Icons.credit_card_rounded),
                        enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.black12),
                        ),
                      ),
                      validator: (value) {
                        if (value!.isEmpty) {
                          return 'Pleaseenteryourmembershipcardnumber'.tr;
                        }
                        return null;
                      },
                    ),
                    TextFormField(
                      controller: OpenIdCardDateController,
                      enabled: false,
                      decoration: InputDecoration(
                        hintText: 'Cardopeningdate'.tr,
                          hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                          prefixIcon: Icon(Icons.date_range),
                        enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.black12),
                        ),
                      ),
                    ),
                  ],
                ),
              ),


            ],
          ),
        ),
      bottomNavigationBar: Container(
        padding: EdgeInsets.all(15),
        child: RaisedButton(
          child: Text('Submitinformation'.tr,style: TextStyle(color: Colors.white),),
          onPressed: () {
            //一步校验全部表单
            if (_formKey.currentState!.validate()) {
              //提交信息
              UploadVip();
            }
          },
          color: Colors.blue,
        ),
      ),
    );
  }
  UploadVip() async {
    //取出本地个人信息
    GetStorage getStorage = new GetStorage();
    var Userid = json.decode(getStorage.read("User"))['user']['userId'];
    //搜索参数
    var data = {
      "name": NameController.text,
      "sex": _groupValue,
      "phone": PhoneController.text,
      "idCard":IdCardController.text,
      "vipCardNumber": VipNumController.text,
      "openCardDate": OpenIdCardDateController.text,
      "operator": Userid,
    };
    KSProgressDialog().LoadingToast("Submitting".tr, context);
    var ResponseData = await VipManage().AddVip(data);
    KSProgressDialog().CloseLoading();
    if (ResponseData['code'] == 200) {
    GetxTools().SuccessToast("Thesubmissionwassuccessful".tr);
    } else {
      new GetxTools().ErrorToast("Commitfailed".tr);
    }
    setState(() {});
  }
}
